/*
* Copyright (c) 2009, Roc King
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the OwnWaterloo,Roc King nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "retrieve.h"
#include "callback_currying/stdcall_partial_application.h"

int CC_STDCALL find_if(int value,node* n) {
    if (n->value==value)
        return terminate_enumerate;
    return continue_enumerate;
}

int CC_STDCALL assgin(int value,node* n) {
    n->value = value;
    return continue_enumerate;
}

typedef struct accumulate_struct_ {
    int sum;
    double prodcut;
} accumulate_struct;

int CC_STDCALL accumulate(accumulate_struct* acc,node* n) {
    acc->sum += n->value;
    acc->prodcut *= n->value;
    return continue_enumerate;
}

int main() {

    node array_list[] = { {86},{1212},{-326},{326},{-1212},{86}, };
    node* const last = array_list + sizeof(array_list)/sizeof(array_list[0]);
    node* p = 0;

    stdcall_partial_application papply;
    int find_key;
    accumulate_struct acc;
    retrieve_fn fn;

    stdcall_initialize(&papply);

    find_key = 1212;
    stdcall_set_applied_function(&papply,(uintptr_t)&find_if);
    stdcall_set_fixed_argument(&papply,find_key);
    assert( (unsigned)find_key == stdcall_get_fixed_argument(&papply) );
    assert( (uintptr_t)&find_if == stdcall_get_applied_function(&papply) );

    fn = (retrieve_fn)(uintptr_t)&papply;
    p = retrieve_array_list(array_list,last,fn);
    if (p == last) {
        printf("not find %d\n",find_key);
    } else  {
        printf("find %d, first index=%d\n",find_key,p - array_list);
    }

    find_key = -326;
    stdcall_set_fixed_argument(&papply,find_key);
    assert( (unsigned)find_key == stdcall_get_fixed_argument(&papply) );
    p = retrieve_array_list(array_list,last,fn);
    if (p == last) {
        printf("not find %d\n",find_key);
    } else  {
        printf("find %d, first index=%d\n",find_key,p - array_list);
    }

    acc.sum = 0;
    acc.prodcut = 1.0;
    stdcall_set_fixed_argument(&papply,(uintptr_t)&acc);
    stdcall_set_applied_function(&papply,(uintptr_t)&accumulate);
    retrieve_array_list(array_list,last,fn);
    printf("accumulate, sum=%d prodcut=%g\n",acc.sum,acc.prodcut);

    stdcall_set_applied_function(&papply,(uintptr_t)&assgin);
    stdcall_set_fixed_argument(&papply,312);
    retrieve_array_list(array_list,last,fn);

    printf("press enter to exit\n");
    (void)getchar();
    return 0;
}
